//
//  HLHomeCell.m
//  weibo-OC
//
//  Created by 黄力 on 15/12/21.
//  Copyright © 2015年 HLTest. All rights reserved.
//

#import "HLHomeCell.h"
#import <Masonry.h>
#import "HLHomeCellTopView.h"
#import "HLHomeCellBottomView.h"
#import "HLHomeCellRetweetView.h"
#import "HLStatuses.h"

@interface HLHomeCell ()
//头部视图
@property (nonatomic,strong) HLHomeCellTopView *homeCellTopView;
//转发视图
@property (nonatomic,strong) HLHomeCellRetweetView *homeCellRetweetView;
//底部视图
@property (nonatomic,strong) HLHomeCellBottomView *homeCellBottomView;
//分隔视图
@property (nonatomic,strong) UIView *homeCellSeparateView;

//底部视图的上约束
@property (nonatomic,strong) MASConstraint *bottomViewTopConstraint;

@end

@implementation HLHomeCell

//重写构造方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    //设置界面
    [self setupUI];
    
    return self;
}

//设置界面
- (void)setupUI {
    //设置cell的选中状态下样式
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    //添加子控件
    [self.contentView addSubview:self.homeCellSeparateView];
    [self.contentView addSubview:self.homeCellTopView];
    [self.contentView addSubview:self.homeCellRetweetView];
    [self.contentView addSubview:self.homeCellBottomView];
    
    //设置约束
    //分隔视图
    [self.homeCellSeparateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.contentView);
        make.height.equalTo(@8);
    }];
    //头部视图
    [self.homeCellTopView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.homeCellSeparateView.mas_bottom);
        make.left.right.equalTo(self.contentView);
    }];
    
    //转发微博
    [self.homeCellRetweetView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.homeCellTopView.mas_bottom);
        make.left.right.equalTo(self.contentView);
    }];
    
    //底部视图
    [self.homeCellBottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        self.bottomViewTopConstraint = make.top.equalTo(self.homeCellRetweetView.mas_bottom);
        make.left.right.equalTo(self.contentView);
        make.height.equalTo(@40);
    }];
    
    //设置contentView约束
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.bottom.equalTo(self.homeCellBottomView);
    }];
}


//重写setter方法
- (void)setStatuses:(HLStatuses *)statuses {
    _statuses = statuses;
    //给原创视图设置数据
    self.homeCellTopView.topSatuses = statuses;
    //给转发视图设置数据
    self.homeCellRetweetView.retweetStatuses = statuses.retweeted_status;
    //写在原有约束
    [self.bottomViewTopConstraint uninstall];
    //根据有没有转发微博来更新约束
    if (statuses.retweeted_status == nil) {
        //隐藏
        self.homeCellRetweetView.hidden = YES;
        [self.homeCellBottomView mas_updateConstraints:^(MASConstraintMaker *make) {
            self.bottomViewTopConstraint = make.top.equalTo(self.homeCellTopView.mas_bottom);
        }];
    }else {
        //显示
        self.homeCellRetweetView.hidden = NO;
        [self.homeCellBottomView mas_updateConstraints:^(MASConstraintMaker *make) {
            self.bottomViewTopConstraint = make.top.equalTo(self.homeCellRetweetView.mas_bottom);
        }];
    }
}

#pragma mark - 懒加载
//头部视图
- (HLHomeCellTopView *)homeCellTopView {
    if (_homeCellTopView == nil) {
        _homeCellTopView = [[HLHomeCellTopView alloc] init];
    }
    return _homeCellTopView;
}

//转发微博视图
- (HLHomeCellRetweetView *)homeCellRetweetView {
    if (_homeCellRetweetView == nil) {
        _homeCellRetweetView = [[HLHomeCellRetweetView alloc] init];
    }
    return _homeCellRetweetView;
}

//底部视图
- (HLHomeCellBottomView *)homeCellBottomView {
    if (_homeCellBottomView == nil) {
        _homeCellBottomView = [[HLHomeCellBottomView alloc] init];
    }
    return _homeCellBottomView;
}

//分隔视图
- (UIView *)homeCellSeparateView {
    if (_homeCellSeparateView == nil) {
        _homeCellSeparateView = [[UIView alloc] init];
        //设置背景颜色
        _homeCellSeparateView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1];
    }
    return _homeCellSeparateView;
}

@end
